package cn.westv.scps.web.controller.areaserver;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.westv.scps.constant.ConfigCodeConstant;
import cn.westv.scps.domain.AreaSeverEntity;
import cn.westv.scps.domain.SysConfig;
import cn.westv.scps.domain.SysDict;
import cn.westv.scps.service.areaserver.IAreaServerService;
import cn.westv.scps.service.sys.dict.ISysDictItemService;
import cn.westv.scps.service.sys.dict.ISysDictService;
import cn.westv.scps.utils.Page;
import cn.westv.scps.utils.StringUtils;
import cn.westv.scps.web.controller.areaserver.vo.AreaServerVO;
import cn.westv.scps.web.controller.base.AbstractController;

/**
 * 地区服务器信息管理相关接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sys/server")
public class AreaServerController extends AbstractController<AreaServerController> {

	@Autowired
	private IAreaServerService areaServerService;

	@RequestMapping("/index.cgi")
	public String toIndex_cgi() {
		return "forward:/WEB-INF/page/sys/areaserver/index.jsp";
	}

	@RequestMapping("/edit.cgi")
	public String toEdit_cgi() {
		return "forward:/WEB-INF/page/sys/areaserver/edit.jsp";
	}
	
	/**
	 * 分页查询服务器
	 * @param areaServerVO
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/pagelist")
	public Map<String, Object> serverFindPage(AreaServerVO areaServerVO,Page<AreaSeverEntity> page){
		try {
			String hql = "from AreaSeverEntity where 1 = 1 ";
			List<Object> paramList = new ArrayList<Object>();
			AreaSeverEntity areaSeverEntity = areaServerVO.getAreaSeverEntity();
			
			if(areaSeverEntity != null){
				String serverId = areaSeverEntity.getServerId();
				if(StringUtils.isNotBlank(serverId)){
					hql += " and serverId = ?";
					paramList.add(serverId);
				}
				String name = areaSeverEntity.getServerName();
				if(StringUtils.isNotBlank(name)){
					hql += " and serverName like ?";
					paramList.add("%" + name + "%");
				}
				String areaCode = areaSeverEntity.getAreaCode();
				if(StringUtils.isNotBlank(areaCode)){
					hql += " and areaCode like ?";
					paramList.add("%" + areaCode + "%");
				}
				String contextPath = areaSeverEntity.getContextPath();
				if(StringUtils.isNotBlank(contextPath)){
					hql += " and contextPath like ?";
					paramList.add("%" + contextPath + "%");
				}
				String proxyId = areaSeverEntity.getProxyId();
				if(StringUtils.isNotBlank(proxyId)){
					hql += " and proxyId = ?";
					paramList.add(proxyId);
				}
				String proxySecret = areaSeverEntity.getProxySecret();
				if(StringUtils.isNotBlank(proxySecret)){
					hql += " and proxySecret = ?";
					paramList.add(proxySecret);
				}
				String mqttHostIp = areaSeverEntity.getMqttHostIp();
				if(StringUtils.isNotBlank(mqttHostIp)){
					hql += " and mqttHostIp like ?";
					paramList.add("%" + mqttHostIp + "%");
				}
				String mqttTcpHostPort = areaSeverEntity.getMqttTcpHostPort();
				if(StringUtils.isNotBlank(mqttTcpHostPort)){
					hql += " and mqttTcpHostPort = ?";
					paramList.add(mqttTcpHostPort);
				}
				String mqttSocketHostPort = areaSeverEntity.getMqttSocketHostPort();
				if(StringUtils.isNoneBlank(mqttSocketHostPort)){
					hql += " and mqttSocketHostPort = ?";
					paramList.add(mqttSocketHostPort);
				}
				String mqttAccount =areaSeverEntity.getMqttAccount();
				if(StringUtils.isNotBlank(mqttAccount)){
					hql += " and mqttAccount = ?";
					paramList.add(mqttAccount);
				}
				String mqttPwd = areaSeverEntity.getMqttPwd();
				if(StringUtils.isNotBlank(mqttPwd)){
					hql += " and mqttPwd = ?";
					paramList.add(mqttPwd);
				}
				String insuranceSendEmail = areaSeverEntity.getInsuranceSendEmail();
				if (StringUtils.isNotBlank(insuranceSendEmail)) {
					hql += " and " + " insuranceSendEmail like ?";
					paramList.add("%" + insuranceSendEmail + "%");
				}
				String insuranceCcEmail = areaSeverEntity.getInsuranceCcEmail();
				if (StringUtils.isNotBlank(insuranceCcEmail)) {
					hql += " and " + " insuranceCcEmail like ?";
					paramList.add("%" + insuranceCcEmail + "%");
				}
				String needIsurEmail = areaSeverEntity.getNeedIsurEmail();
				if (StringUtils.isNotBlank(needIsurEmail)) {
					hql += " and " + " needIsurEmail like ?";
					paramList.add("%" + needIsurEmail + "%");
				}
			}
			Page<AreaSeverEntity> findPage = areaServerService.findPage(hql, page, AreaSeverEntity.class, paramList.toArray());
			
			List<AreaSeverEntity> results = findPage.getResults();
			for (AreaSeverEntity areaSever : results) {
				//解决映射死循环问题
				areaSever.setBikeOners(null);
				areaSever.setSysMassageSendRecordEntitys(null);
				areaSever.setPublishAgentEntitys(null);
				//将敏感字段设置为null
				areaSever.setProxyId(null);
				areaSever.setProxySecret(null);
				areaSever.setMqttAccount(null);
				areaSever.setMqttPwd(null);
				areaSever.setInsuranceCcEmail(null);
				areaSever.setInsuranceSendEmail(null);
				areaSever.setNeedIsurEmail(null);
			}
			return ajaxReturnMap(OP_STATUS_SUCCESS, "search successful!", findPage);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
	}
	
	/**
	 * 保存
	 * @param areaServerVO
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> saveAreaServer(AreaServerVO areaServerVO){
		try {			
			if(areaServerVO.getAreaSeverEntity() != null){
				AreaSeverEntity areaSeverEntity = areaServerVO.getAreaSeverEntity();
//				AreaSeverEntity areaSeverEntityDB = null;
				String msg = "";//提示信息
				//serverId为空-添加
				if(StringUtils.isBlank(areaSeverEntity.getServerId())){
					
					String areaCode = areaSeverEntity.getAreaCode();
					List<AreaSeverEntity> findCode = areaServerService.find("from AreaSeverEntity where areaCode = ?", AreaSeverEntity.class, new Object[] {areaCode});
					//服务器编码唯一
					if(findCode != null && findCode.size() > 0){
						return ajaxReturnMap(OP_STATUS_FAILD, "添加失败，服务器编码重复！",areaCode);
					}
					
//					areaSeverEntityDB = areaSeverEntity;
					msg = ConfigCodeConstant.ADD_SUCCEED;// 添加成功
					areaServerService.save(areaSeverEntity);
				}else {
					//serverId不为空-修改
//					String serverId = areaSeverEntity.getServerId();
//					areaSeverEntityDB = areaServerService.get(AreaSeverEntity.class, serverId);
					
					msg = ConfigCodeConstant.UPDATE_SUCCEED;//修改成功
					areaServerService.update(areaSeverEntity);
				}

				return ajaxReturnMap(OP_STATUS_SUCCESS, msg, null);
			}else{
				return ajaxReturnMap(OP_STATUS_FAILD, "Save failed!", null);
			}
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}

	}
	
	/**
	 * 删除
	 * @param ids 多个id之间用","分隔
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Map<String, Object> delAreaServer(String ids){
		try {
			String[] idArr = null;
			if (StringUtils.isNotBlank(ids)) {
				idArr = ids.split(","); //根据id批量删除，id之间使用","分隔
			}
			String msg = "";
			areaServerService.delete(AreaSeverEntity.class, idArr);
			msg = ConfigCodeConstant.DEL_SUCCEED;
			return ajaxReturnMap(OP_STATUS_SUCCESS, msg, null);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}
	
	/**
	 * 根据Id查找
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/detail")
	public Map<String, Object> detailById(String id) {
		if (StringUtils.isEmpty(id)) {
			return ajaxReturnMap(OP_STATUS_FAILD, ConfigCodeConstant.REQUIRED_PARAMETERS_IS_NULL + "（id为空）", null);
		}
		try {
			AreaSeverEntity entity = areaServerService.get(AreaSeverEntity.class, id);
			//解决映射 死循环问题
			entity.setBikeOners(null);
			entity.setSysMassageSendRecordEntitys(null);
			entity.setPublishAgentEntitys(null);
			
			return ajaxReturnMap(OP_STATUS_SUCCESS, "", entity);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}
	
	/**
	 *  根据条件查询所有
	 * @param areaServerVO
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public Map<String, Object> find(AreaServerVO areaServerVO){
		try {
			List<AreaSeverEntity> list = null;
			List<Object> paramList = new ArrayList<Object>();
			String hql = "from AreaSeverEntity where 1 = 1";
			AreaSeverEntity areaSeverEntity = areaServerVO.getAreaSeverEntity();

			if(areaSeverEntity != null){
				String serverId = areaSeverEntity.getServerId();
				if(StringUtils.isNotBlank(serverId)){
					hql += " and serverId = ?";
					paramList.add(serverId);
				}
				String name = areaSeverEntity.getServerName();
				if(StringUtils.isNotBlank(name)){
					hql += " and serverName like ?";
					paramList.add("%" + name + "%");
				}
				String areaCode = areaSeverEntity.getAreaCode();
				if(StringUtils.isNotBlank(areaCode)){
					hql += " and areaCode like ?";
					paramList.add("%" + areaCode + "%");
				}
				String contextPath = areaSeverEntity.getContextPath();
				if(StringUtils.isNotBlank(contextPath)){
					hql += " and contextPath like ?";
					paramList.add("%" + contextPath + "%");
				}
				String proxyId = areaSeverEntity.getProxyId();
				if(StringUtils.isNotBlank(proxyId)){
					hql += " and proxyId = ?";
					paramList.add(proxyId);
				}
				String proxySecret = areaSeverEntity.getProxySecret();
				if(StringUtils.isNotBlank(proxySecret)){
					hql += " and proxySecret = ?";
					paramList.add(proxySecret);
				}
				String mqttHostIp = areaSeverEntity.getMqttHostIp();
				if(StringUtils.isNotBlank(mqttHostIp)){
					hql += " and mqttHostIp like ?";
					paramList.add("%" + mqttHostIp + "%");
				}
				String mqttTcpHostPort = areaSeverEntity.getMqttTcpHostPort();
				if(StringUtils.isNotBlank(mqttTcpHostPort)){
					hql += " and mqttTcpHostPort = ?";
					paramList.add(mqttTcpHostPort);
				}
				String mqttSocketHostPort = areaSeverEntity.getMqttSocketHostPort();
				if(StringUtils.isNoneBlank(mqttSocketHostPort)){
					hql += " and mqttSocketHostPort = ?";
					paramList.add(mqttSocketHostPort);
				}
				String mqttAccount =areaSeverEntity.getMqttAccount();
				if(StringUtils.isNotBlank(mqttAccount)){
					hql += " and mqttAccount = ?";
					paramList.add(mqttAccount);
				}
				String mqttPwd = areaSeverEntity.getMqttPwd();
				if(StringUtils.isNotBlank(mqttPwd)){
					hql += " and mqttPwd = ?";
					paramList.add(mqttPwd);
				}
				String insuranceSendEmail = areaSeverEntity.getInsuranceSendEmail();
				if (StringUtils.isNotBlank(insuranceSendEmail)) {
					hql += " and " + " insuranceSendEmail like ?";
					paramList.add("%" + insuranceSendEmail + "%");
				}
				String insuranceCcEmail = areaSeverEntity.getInsuranceCcEmail();
				if (StringUtils.isNotBlank(insuranceCcEmail)) {
					hql += " and " + " insuranceCcEmail like ?";
					paramList.add("%" + insuranceCcEmail + "%");
				}
				String needIsurEmail = areaSeverEntity.getNeedIsurEmail();
				if (StringUtils.isNotBlank(needIsurEmail)) {
					hql += " and " + " needIsurEmail like ?";
					paramList.add("%" + needIsurEmail + "%");
				}
			}
			list = areaServerService.find(hql, AreaSeverEntity.class, paramList.toArray());

			for (AreaSeverEntity area : list) {
				//解决映射 死循环问题
				area.setBikeOners(null);
				area.setSysMassageSendRecordEntitys(null);
				area.setPublishAgentEntitys(null);
				
			}
			JSONArray parseArray = JSON.parseArray(JSON.toJSONString(list));
			return ajaxReturnMap(OP_STATUS_SUCCESS, "" ,parseArray);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
	}

	
}
